import { reqAddressInfo, reqShopInfo, reqSubmitOrder } from "@/api";

const state = {
    // 用户地址信息
    address: [],
    // 商品清单数据
    tradeInfo: {},
    // 订单id
    payId: '',
};

const mutations = {
    GETADDRESS(state, address) {
        state.address = address;
    },

    GETSHOPINFO(state, tradeInfo) {
        state.tradeInfo = tradeInfo;
    },
    SUBMITINFO(state, payId) {
        state.payId = payId;
    }
};

const actions = {
    // 获取用户信息地址
    async getAddress({ commit, state, dispatch }) {
        let result = await reqAddressInfo();
        if (result.code == 200) {
            commit('GETADDRESS', result.data)
            return 'ok'


        } else {
            return Promise.reject(new Error(result.message))
        }

    },

    // 获取商品清单的数据
    async getShopInfo({ commit, state, dispatch }) {
        let result = await reqShopInfo();
        if (result.code == 200) {
            commit('GETSHOPINFO', result.data)
            return 'ok'


        } else {
            return Promise.reject(new Error(result.message))
        }
    },


    // 提交订单
    async submitInfo({ commit, state, dispatch }, { tradeNo, data }) {
        let result = await reqSubmitOrder(tradeNo, data);
        if (result.code == 200) {
            commit('SUBMITINFO', result.data)
            return 'ok'


        } else {
            return Promise.reject(new Error(result.message))
        }
    },
};

const getters = {};


export default {
    state,
    mutations,
    actions,
    getters
}